package com.iwhalecloud.bss.kite.cucc.order.bo.flow;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.OrderFlowConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferFlowConfigCache;
import com.iwhalecloud.bss.kite.dataservice.cache.TacheTypeCache;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.Flow;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.OfferFlowConfig;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.TacheType;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.mapper.flow.FlowMapper;
import com.iwhalecloud.bss.kite.order.client.dto.OrderFlowRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * @author yu.baocheng<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020/2/22 11:59 下午 <br>
 * @since ZSmart_BSS_COC_R3.1<br>
 * <Description>
 * 订单流程BO类
 * <br>
 */
@Component
public class OrderFlowQueryBO {

    @Autowired
    FlowMapper flowMapper;

    /**
     * 查询第一个环节TacheType定义；
     * @author TinaYuzuKi<br>
     * @taskId <br>
     * @CreateDate 2020/2/23 12:09 上午 <br>
     * @since ZSmart_BSS_COC_R3.1<br>
     * @return: java.util.List<com.iwhalecloud.bss.kite.dataservice.entity.flow.TacheType>
     */
    public TacheType getFirstTacheType(String offerId, String serviceOfferId) {
        List<TacheType> tacheTypes = this.listAllTacheType(offerId, serviceOfferId);
        TacheType tacheType = tacheTypes.stream().findFirst().filter(t -> KiteStringUtils.equals(t.getTacheTypeKind(), OrderFlowConsts.TACHE_TYPE_KIND_START)).orElseGet(null);
        if (Objects.isNull(tacheType)) {
            ErrorConsts.ORDER_FLOW_ERROR_FIRSTTACHETYPE_NOT_EXISTS_ERROR.throwOut(offerId, serviceOfferId);
        }
        return tacheType;
    }
    
    /**
     * Description: <br>
     * 根据流程类型查询第一个环节TacheType定义；
     * 
     * @author TinaYuzuKi<br>
     * @taskId <br>
     * @param flowTypeId
     * @return <br>
     */
    public TacheType getFirstTacheTypeByFlowTypeId(String flowTypeId) {
        TacheType tacheType = null;
        List<TacheType> tacheTypes = TacheTypeCache.listTacheTypeByFlowTypeId(flowTypeId);
        if (KiteListUtils.isNotEmpty(tacheTypes)) {
            tacheType = tacheTypes.stream().findFirst()
                .filter(t -> KiteStringUtils.equals(t.getTacheTypeKind(), OrderFlowConsts.TACHE_TYPE_KIND_START))
                .orElseGet(null);
        }
        if (Objects.isNull(tacheType)) {
            ErrorConsts.ORDER_FLOW_ERROR_TACHETYPE_NOT_EXISTS_ERROR.throwOut(flowTypeId);
        }
        return tacheType;
    }

    /**
     * 获取订单流程的第一个环节；
     * @author yu.baocheng@iwhalecloud.com<br>
     * @taskId <br>
     * @CreateDate 2020/2/27 2:46 上午 <br>
     * @since ZSmart_BSS_COC_R3.1<br>
     * @param offerId:
     * @param serviceOfferId:
     * @return: com.iwhalecloud.bss.kite.dataservice.entity.flow.TacheType
     */
    public List<TacheType> listAllTacheType(String offerId, String serviceOfferId) {
        OfferFlowConfig offerFlowConfig = this.getOfferFlowConfig(offerId, serviceOfferId);
        List<TacheType> tacheTypes = TacheTypeCache.listTacheTypeByFlowTypeId(offerFlowConfig.getFlowTypeId());
        if (KiteListUtils.isEmpty(tacheTypes)) {
            ErrorConsts.ORDER_FLOW_ERROR_TACHETYPE_NOT_EXISTS_ERROR.throwOut(offerFlowConfig.getFlowTypeId());
        }
        return tacheTypes;
    }

    /***
     * 获取销售品配置的订单流程；
     * @author yu.baocheng@iwhalecloud.com<br>
     * @taskId <br>
     * @CreateDate 2020/2/27 2:34 下午 <br>
     * @since ZSmart_BSS_COC_R3.1<br>
     * @param offerId:
     * @return: java.util.List<com.iwhalecloud.bss.kite.dataservice.entity.flow.OfferFlowConfig>
     */
    public List<OfferFlowConfig> listAllOfferFlowConfigsByOfferId(String offerId) {
        List<OfferFlowConfig> offerFlowConfigs = OfferFlowConfigCache.get(offerId);
        Offer offer = OfferCache.get(offerId);
        offerFlowConfigs.addAll(OfferFlowConfigCache.listOfferFlowConfigByOfferType(offer.getOfferType()));
        return offerFlowConfigs;
    }

    /***
     * 通过销售品Id和服务提供获取销售品订单流程配置；
     * @author yu.baocheng@iwhalecloud.com<br>
     * @taskId <br>
     * @CreateDate 2020/2/27 4:50 下午 <br>
     * @since ZSmart_BSS_COC_R3.1<br>
     * @param offerId:
     * @param serviceOfferId:
     * @return: com.iwhalecloud.bss.kite.dataservice.entity.flow.OfferFlowConfig
     */
    public OfferFlowConfig getOfferFlowConfig(String offerId, String serviceOfferId) {
        List<OfferFlowConfig> offerFlowConfigs = this.listAllOfferFlowConfigsByOfferId(offerId);
        OfferFlowConfig offerFlowConfig = offerFlowConfigs.stream()
            .filter(t -> KiteStringUtils.equals(t.getServiceOfferId(), serviceOfferId)).findFirst().orElse(null);
        if (Objects.isNull(offerFlowConfig)) {
            ErrorConsts.ORDER_FLOW_ERROR_CONFIG_NOT_EXISTS_ERROR.throwOut(offerId);
        }
        return offerFlowConfig;
    }

    /***
     * 校验数据是否存在；
     * @param request
     * @return
     */
    public Flow isExistsFlow(OrderFlowRequest request) {

        Example example = new Example(Flow.class);
        example.createCriteria()
            .andNotEqualTo("flowTypeId", request.getFlowTypeId())
            .andEqualTo("custId", request.getCustId())
            .andEqualTo("instId", request.getOrderItem().getOrderItemId())
            .andEqualTo("instType", OrderFlowConsts.FLOW_INST_TYPE_ORDITEM);
        Flow flow = flowMapper.selectOneByExample(example);

        if (Objects.nonNull(flow) && KiteStringUtils.isNotEmpty(flow.getFlowId())) {
            //数据存在；
            return flow;
        }
        return null;
    }
}
